-- skill_get_soul
-- created by zhuangsl
-- 使用特定技能击杀敌人概率拘禁灵魂

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 击杀触发
    trigger = function(who, prop, arg)
        local bonus;
        local monster = arg;

        -- 不是怪物
        if monster.type ~= OBJECT_TYPE_MONSTER then
            return false;
        end

        -- 灵魂已经没了
        if monster.dbase:query("soulless") == 1 then
            return false;
        end

        -- 概率不符合
        if DungeonM.getRandSeed("skill_get_soul") % 1000 >= tonumber(prop[3]) then
            return false;
        end

        local monsterId = monster.classId;
        local soulItem = MonsterM.query(monsterId, "soul_item");
        local prefix = "formula_";
        local itemId;

        if type(soulItem) == "number" then
            itemId = soulItem;
        elseif type(soulItem) == "string" and
               string.startWith(soulItem, prefix) then
            local formulaName = string.sub(soulItem, string.len(prefix) + 1);
            itemId = FormulaM.invoke(formulaName);
        end

        if type(itemId) == "number" then
            -- 标记为没有灵魂了
            monster.dbase:set("soulless", 1);

            bonus = { 1, itemId, 1, };
            BonusM.doBonus(bonus, "skill_get_soul");

            local pos = monster:getPos();

            -- 抛出玩家获得物品的事件
            EventMgr.fire(event.BONUS_ITEM, { ["classId"] = itemId, ["amount"] = 1, ["pos"] = pos,});
        end

        return true;
    end,

    record = function(target, prop, path)
        return false;
    end
};